package com.voxel.sense.infomsg.utils;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.example.com.common.utils.FileUtils;
import com.example.com.common.utils.ToastUtil;

import java.io.File;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.Constants;

/**
 * Created by ChenYiYao on 2017/12/26.
 */

public class OpenDocuIntentUtils {
    public static void DocuItent(Context context, File file, String type, String fileExtension) {
        if (FileUtils.isFile(file) && !TextUtils.isEmpty(fileExtension)) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addCategory("android.intent.category.DEFAULT");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            FileProvider7.setIntentDataAndType(intent, type, file, false);
            try {
                if (intent.resolveActivity(context.getPackageManager()) != null) {
                    context.startActivity(intent);
                } else {
                    ToastUtil.showToastLong(Factory.app(), "无法找到打开该文档的应用");
                }
            } catch (Exception e) {
                e.printStackTrace();
                ToastUtil.showToastLong(Factory.app(), "无法找到打开该文档的应用");
            }
        } else {
            FileUtils.deleteFile(file);
            FileUtils.deleteDir(file);
            ToastUtil.showToastLong(Factory.app(), "文件不存在或已被删除");
        }
    }

    public static void OpenFile(File file, Context context) {
        String type = "text/plain";//默认文本类型
        String fileExtension = FileUtils.getFileExtension(file);
        if (Constants.EXTENSION_DOC.equals(fileExtension) || Constants.EXTENSION_DOCX.equals(fileExtension)) {
            //doc文档
            type = "application/msword";
        } else if (Constants.EXTENSION_XLS.equals(fileExtension) || Constants.EXTENSION_XLSX.equals(fileExtension)) {
            //xls文档
            type = "application/vnd.ms-excel";
        } else if (Constants.EXTENSION_PDF.equals(fileExtension)) {
            //pdf文档
            type = "application/pdf";
        } else if (Constants.EXTENSION_PPT.equals(fileExtension) || Constants.EXTENSION_PPTX.equals(fileExtension)) {
            //ppt文档
            type = "application/vnd.ms-powerpoint";
        }
        OpenDocuIntentUtils.DocuItent(context, file, type, fileExtension);
    }
}
